package tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

@Slf4j
public class QrUtil {

    public static File QrCode(String lotteryUrl, Long userNo)  {
        QrConfig qrConfig = new QrConfig(500, 500);
        //纠错级别。低级别的像素块更大，可以远距离识别，但是遮挡就会造成无法识别。高级别则相反，像素块小，允许遮挡一定范围，但是像素块更密集
        qrConfig.setErrorCorrection(ErrorCorrectionLevel.H);
        URL url= null;
        try {
            url = new URL("https://lottery-1301612338.cos.ap-guangzhou.myqcloud.com/black-logo.png");
        } catch (MalformedURLException e) {
            log.error("url不存在",e);
        }
        Image image = null;
        try {
            assert url != null;
            image = ImageIO.read(url);
        } catch (IOException e) {
            log.error("io异常",e);
        }
        qrConfig.setImg(image);
        return QrCodeUtil.generate(lotteryUrl, qrConfig, FileUtil.file("G:/Users/35396/IdeaProjects/QR_lottery/tmp/" + userNo + ".jpg"));
    }

}
